﻿namespace ProcessLib.Ratp.Page.Py;

public class PreSemanticCutter
{
    public static PySemanticCutterResult Cutter(List<string> lines)
    {
        var pyRes = new PySemanticCutterResult();
        pyRes.Source = lines;
        var list = new List<string>();
        var paragraphs = new List<PySemanticParagraph>();
        foreach (var line in lines)
        {

            if (line.Length == 1)
            {
                //单个字母，和后面组合作为预处理的段落
                AddNewBlock();//清空或者不做任何事情
                list.Add(line);
            }else if (End(line))
            {
                list.Add(line);
                AddNewBlock();
            }
            else if (!line.Trim().Contains(' '))
            {
                // 单个单词，作为单行
                AddNewBlock();
                list.Add(line);
                AddNewBlock();
            }
            else
            {
                list.Add(line);
            }
        }

        pyRes.Semantics = paragraphs;
        return pyRes;

        void AddNewBlock()
        {
            if (list.Count > 0)
            {
                var newList = new List<string>();
                newList.AddRange(list);
                var paragraph = new PySemanticParagraph
                {
                    RegionLines = newList
                };
                paragraphs.Add(paragraph);
                list.Clear();
            }
        }
    }

    private static bool End(string line)
    {
        var c = line.Trim().Last();
        return c == '!' || c == '.' || c == '?' || c == '。' || c == '？' || c == '!';
    }

}